package edu.uoc.tdp.pac4.client.maintenance;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.HeadlessException;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import edu.uoc.tdp.pac4.beans.Aula;
import edu.uoc.tdp.pac4.beans.TipusAula;
import edu.uoc.tdp.pac4.client.ClientConnectionHelper;
import edu.uoc.tdp.pac4.common.MantenimentInterface;
import edu.uoc.tdp.pac4.common.util.LanguageUtils;


public class ClassroomEditWindow extends JFrame {
	//MantenimentInterface mantInterf=new MantenimentImpl();
	//private static final long serialVersionUID = 1L;
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yy");
	private ClientConnectionHelper connectionHelper_;
	private MantenimentInterface maintenance;

	
	private JTextField tfId;
	private JTextField tfName;
	private JTextField tfCapacity;
	//private JTextArea tABibl, tAPlaDoc;	
	//private JTextField tfClassroomType;
	private JTextField tfLocation;
	private JComboBox<TipusAula> cbTipusAula;
	
	private ButtonGroup groupStatus = new ButtonGroup();
	
	private final ButtonGroup buttonGroup = new ButtonGroup();

	private Aula classroom;


	
	//Date avui = new Date(System.currentTimeMillis());
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		LanguageUtils.setDefaultLanguage("i18n/messages");
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ClassroomEditWindow frame = new ClassroomEditWindow(new ClientConnectionHelper());
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 * @param connectionHelper_ 
	 */
	
	public ClassroomEditWindow(ClientConnectionHelper connectionHelper_)
			throws HeadlessException {
		this(null, connectionHelper_);
	}
	
	/**
	 * @wbp.parser.constructor
	 */
	public ClassroomEditWindow(final Aula classroom, ClientConnectionHelper connectionHelper_) {

		this.classroom = classroom;
		this.connectionHelper_ = connectionHelper_;
		this.maintenance=connectionHelper_.getMaintenanceManager();
		
		setTitle("eAssistencia - Classroom edit");
		getContentPane().setLayout(null);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 358);
		
		
		JPanel panelSup = new JPanel();
		panelSup.setBounds(0, 0, 315, 247);
		getContentPane().add(panelSup);
		GridBagLayout gbl_panelSup = new GridBagLayout();
		gbl_panelSup.rowHeights = new int[] {30, 30, 30, 30, 30, 30};
		gbl_panelSup.columnWidths = new int[] {30, 90, 90, 30};
		gbl_panelSup.columnWeights = new double[]{0.0, 0.0, 0.0, 1.0};
		gbl_panelSup.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0};
		panelSup.setLayout(gbl_panelSup);
		
		JLabel labelId = new JLabel(" Id");
		labelId.setHorizontalAlignment(SwingConstants.LEFT);
		GridBagConstraints gbc_labelId = new GridBagConstraints();
		gbc_labelId.fill = GridBagConstraints.HORIZONTAL;
		gbc_labelId.anchor = GridBagConstraints.WEST;
		gbc_labelId.insets = new Insets(0, 0, 5, 5);
		gbc_labelId.gridx = 1;
		gbc_labelId.gridy = 0;
		panelSup.add(labelId, gbc_labelId);
		
		tfId = new JTextField();
		GridBagConstraints gbc_tfId = new GridBagConstraints();
		gbc_tfId.fill = GridBagConstraints.HORIZONTAL;
		gbc_tfId.insets = new Insets(0, 0, 5, 5);
		gbc_tfId.gridx = 2;
		gbc_tfId.gridy = 0;
		panelSup.add(tfId, gbc_tfId);
		tfId.setColumns(1);
		
		JLabel labelName = new JLabel(LanguageUtils.getMessage("client.maintenance.usermanag.labelName"));
		labelName.setHorizontalAlignment(SwingConstants.LEFT);
		GridBagConstraints gbc_labelName = new GridBagConstraints();
		gbc_labelName.fill = GridBagConstraints.HORIZONTAL;
		gbc_labelName.anchor = GridBagConstraints.WEST;
		gbc_labelName.insets = new Insets(0, 0, 5, 5);
		gbc_labelName.gridx = 1;
		gbc_labelName.gridy = 1;
		panelSup.add(labelName, gbc_labelName);
		
		tfName = new JTextField();
		GridBagConstraints gbc_tfName = new GridBagConstraints();
		gbc_tfName.fill = GridBagConstraints.HORIZONTAL;
		gbc_tfName.insets = new Insets(0, 0, 5, 5);
		gbc_tfName.gridx = 2;
		gbc_tfName.gridy = 1;
		panelSup.add(tfName, gbc_tfName);
		tfName.setColumns(10);
		
		
		JLabel labelCapacity = new JLabel(LanguageUtils.getMessage("client.maintenance.classmanag.labelCapacity"));
		labelCapacity.setHorizontalAlignment(SwingConstants.LEFT);
		GridBagConstraints gbc_labelCapacity = new GridBagConstraints();
		gbc_labelCapacity.anchor = GridBagConstraints.WEST;
		gbc_labelCapacity.insets = new Insets(0, 0, 5, 5);
		gbc_labelCapacity.gridx = 1;
		gbc_labelCapacity.gridy = 2;
		panelSup.add(labelCapacity, gbc_labelCapacity);
		
		tfCapacity = new JTextField();
		GridBagConstraints gbc_tfCapacity = new GridBagConstraints();
		gbc_tfCapacity.fill = GridBagConstraints.HORIZONTAL;
		gbc_tfCapacity.insets = new Insets(0, 0, 5, 5);
		gbc_tfCapacity.gridx = 2;
		gbc_tfCapacity.gridy = 2;
		panelSup.add(tfCapacity, gbc_tfCapacity);
		tfCapacity.setColumns(10);
		
		JLabel labelClassroomType = new JLabel(LanguageUtils.getMessage("client.maintenance.classmanag.labelType"));
		labelClassroomType.setHorizontalAlignment(SwingConstants.LEFT);
		GridBagConstraints gbc_labelClassroomType = new GridBagConstraints();
		gbc_labelClassroomType.anchor = GridBagConstraints.WEST;
		gbc_labelClassroomType.insets = new Insets(0, 0, 5, 5);
		gbc_labelClassroomType.gridx = 1;
		gbc_labelClassroomType.gridy = 3;
		panelSup.add(labelClassroomType, gbc_labelClassroomType);
		
		
		
		
		
//		tfClassroomType = new JTextField();
//		tfClassroomType.setColumns(10);
//		GridBagConstraints gbc_tfClassroomType = new GridBagConstraints();
//		gbc_tfClassroomType.insets = new Insets(0, 0, 5, 5);
//		gbc_tfClassroomType.fill = GridBagConstraints.HORIZONTAL;
//		gbc_tfClassroomType.gridx = 2;
//		gbc_tfClassroomType.gridy = 3;
//		panelSup.add(tfClassroomType, gbc_tfClassroomType);

		cbTipusAula = new JComboBox(TipusAula.values());
		GridBagConstraints gbc_cbTipusAula = new GridBagConstraints();
		gbc_cbTipusAula.insets = new Insets(0, 0, 5, 5);
		gbc_cbTipusAula.fill = GridBagConstraints.HORIZONTAL;
		gbc_cbTipusAula.gridx = 2;
		gbc_cbTipusAula.gridy = 3;
		panelSup.add(cbTipusAula, gbc_cbTipusAula);
		
				
		
		JLabel labelLocation = new JLabel(LanguageUtils.getMessage("client.maintenance.classmanag.labelLocation"));
		labelLocation.setHorizontalAlignment(SwingConstants.LEFT);
		GridBagConstraints gbc_labelLocation = new GridBagConstraints();
		gbc_labelLocation.anchor = GridBagConstraints.WEST;
		gbc_labelLocation.insets = new Insets(0, 0, 5, 5);
		gbc_labelLocation.gridx = 1;
		gbc_labelLocation.gridy = 4;
		panelSup.add(labelLocation, gbc_labelLocation);
		
		
		tfLocation = new JTextField();
		tfLocation.setColumns(10);
		GridBagConstraints gbc_tfLocation = new GridBagConstraints();
		gbc_tfLocation.insets = new Insets(0, 0, 5, 5);
		gbc_tfLocation.fill = GridBagConstraints.HORIZONTAL;
		gbc_tfLocation.gridx = 2;
		gbc_tfLocation.gridy = 4;
		panelSup.add(tfLocation, gbc_tfLocation);
		
		
		
		
		JRadioButton rdbtnActive = new JRadioButton("active");
		GridBagConstraints gbc_rdbtnActive = new GridBagConstraints();
		gbc_rdbtnActive.anchor = GridBagConstraints.WEST;
		gbc_rdbtnActive.insets = new Insets(0, 0, 0, 5);
		gbc_rdbtnActive.gridx = 1;
		gbc_rdbtnActive.gridy = 5;
		panelSup.add(rdbtnActive, gbc_rdbtnActive);
		
		JRadioButton rdbtnInactive = new JRadioButton("inactive");
		GridBagConstraints gbc_rdbtnInactive = new GridBagConstraints();
		gbc_rdbtnInactive.anchor = GridBagConstraints.WEST;
		gbc_rdbtnInactive.gridx = 2;
		gbc_rdbtnInactive.gridy = 5;
		panelSup.add(rdbtnInactive, gbc_rdbtnInactive);
		
		groupStatus.add(rdbtnActive);
		groupStatus.add(rdbtnInactive);

		

		
		JButton btnSave = new JButton(LanguageUtils.getMessage("client.maintenance.classmanag.btnSave"));
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					Aula classroomX= new Aula();
					//usuari.setContrasenya(txtPassword.getText());
					classroomX.setNom(tfName.getText());
					classroomX.setCapacitat(Integer.valueOf(tfCapacity.getText()));
					classroomX.setLocalitzacio(tfLocation.getText());
					classroomX.setTipusAula((TipusAula)cbTipusAula.getSelectedItem());
					//classroomX.setDataInactivitat(dateFormat.parse( tfName.getText()));
					
					if(classroom == null){
						createClassroom(classroomX);			
					} else {
						classroomX.setIdAula(classroom.getIdAula());
						updateClassroom(classroomX);
					}
					dispose();
				} catch (IllegalArgumentException ex) {
					JOptionPane.showMessageDialog(null, ex.getMessage(), null,
							JOptionPane.INFORMATION_MESSAGE);			
				} catch (RemoteException e1) {
					//LanguageUtils.getMessage("client.management.button.group.saved")
					e1.printStackTrace();
				} 
				catch (ParseException e1) {
					JOptionPane.showMessageDialog(null, e1.getMessage(), null,
							JOptionPane.INFORMATION_MESSAGE);			
				}
				
			}


		});
		GridBagConstraints gbc_btnSave = new GridBagConstraints();
		gbc_btnSave.fill = GridBagConstraints.HORIZONTAL;
		gbc_btnSave.insets = new Insets(0, 0, 5, 5);
		gbc_btnSave.gridx = 1;
		gbc_btnSave.gridy = 6;
		panelSup.add(btnSave, gbc_btnSave);
		


		
		JButton btnExit = new JButton(LanguageUtils.getMessage("client.maintenance.usermanag.btnCancel"));
		btnExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		btnExit.setForeground(Color.RED);
		GridBagConstraints gbc_btnExit = new GridBagConstraints();
		gbc_btnExit.fill = GridBagConstraints.HORIZONTAL;
		gbc_btnExit.insets = new Insets(0, 0, 5, 5);
		gbc_btnExit.gridx = 2;
		gbc_btnExit.gridy = 6;
		panelSup.add(btnExit, gbc_btnExit);
		
		
		
		
		if(classroom!=null) {
			System.out.println(classroom);
			tfId.setText(""+classroom.getIdAula());//getDataBaixa().toString()
			tfId.setEnabled(false);
			tfName.setText(classroom.getNom());
			tfLocation.setText(classroom.getLocalitzacio());
			tfCapacity.setText(""+classroom.getCapacitat());

			cbTipusAula.setSelectedItem(classroom.getTipusAula());
			
			
			if(classroom.isActive()){
				rdbtnActive.setSelected(true);				
			} else {
				rdbtnInactive.setSelected(true);		
			}

			
		}
		
				
	}		
		

	private Aula createClassroom(Aula classroomX) throws RemoteException, ParseException {
		Aula classroom = maintenance.createClassroom(classroomX);
		return classroomX;		
	}
	
	protected void updateClassroom(Aula classroomX) throws RemoteException {
		maintenance.updateClassroom(classroomX);
	}
		
		
	
}


